package no.awerup.armory_miner.collectors;

import java.io.IOException;
import java.io.StringReader;

import javax.xml.parsers.DocumentBuilder;
import javax.xml.parsers.ParserConfigurationException;
import javax.xml.xpath.XPath;
import javax.xml.xpath.XPathExpressionException;
import javax.xml.xpath.XPathFactory;

import no.awerup.armory_miner.AMClient;

import org.apache.camel.Exchange;
import org.w3c.dom.Document;
import org.xml.sax.InputSource;
import org.xml.sax.SAXException;

import domain.Char;
import domain.Item;

public class ItemCollector extends Collector {

	public void process(Exchange exchange) throws Exception {
		String inBody = (String) exchange.getIn().getBody( String.class );
		System.out.println( inBody);

		// TODO: Parse body
		String itemId = "37784";
		
		
		collectItemData( itemId );
	}

	void collectItemData(String itemId) throws XPathExpressionException, ParserConfigurationException, SAXException, IOException {
		AMClient amClient = createAMClient();    
		System.out.println( "Getting item info for itemId '" + itemId + "'" );
    	String gs = amClient.getItem(itemId);
    	System.out.println( gs );
    	
    	Item item = parseItemXMLtoItem( gs );
		
	}

	Item parseItemXMLtoItem(String itemXML) throws ParserConfigurationException, SAXException, IOException, XPathExpressionException {
		DocumentBuilder builder = createDocumentBuilder();
		Document doc = builder.parse(new InputSource(new StringReader(itemXML)));
		
		XPathFactory factory = XPathFactory.newInstance();
		XPath xpath = factory.newXPath();
		
		javax.xml.xpath.XPathExpression classNameXpath = xpath.compile("//characterInfo/character/@class");
		
		return null;
	}

}
